package cn.edu.zju.gis.disasterBE.sever.dao;

import cn.edu.zju.gis.disasterBE.sever.entity.User;
import cn.edu.zju.gis.disasterBE.sever.entity.assistants.UserAssistant;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserAssistantDAO extends BaseMapper<UserAssistant> {

    @Select("select * from users_assistant where mailaddr = #{mailAddr}")
    UserAssistant getByMail(String mailAddr);

    @Select("select * from users_assistant order by applytime asc")
    List<UserAssistant> getApplications();

    @Delete("delete from users_assistant where mailaddr = #{mailAddr}")
    int deleteByMail(String mailAddr);

    default boolean isExist(String mailAddr)
    {
        try {
            UserAssistant userAssistant = getByMail(mailAddr);
            if(userAssistant.getMailAddr() == null)
                System.out.println("null");
        }catch (NullPointerException e)
        {
            //System.out.println("not exist");
            return false;
        }

        return true;
    }

    default boolean insertUsers(UserAssistant userAssistant)
    {
        if (isExist(userAssistant.getMailAddr()))
            return false;
        int ans = this.insert(userAssistant);
        System.out.println(ans);
        return ans > 0;
    }
}
